package com.hnust.service.impl;

import com.hnust.dao.CollegeAdminDao;
import com.hnust.dao.CollegeDao;
import com.hnust.dao.SchoolAdminDao;
import com.hnust.domain.CollegeAdmin;
import com.hnust.domain.SchoolAdmin;
import com.hnust.domain.record.AdminRecord;
import com.hnust.service.LoginService;
import com.hnust.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("loginService")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private CollegeAdminDao collegeAdminDao;

    @Autowired
    private SchoolAdminDao schoolAdminDao;

    @Autowired
    private CollegeDao collegeDao;

    /**
     * 院管理员登陆
     * @param account 账号
     * @param password 密码
     * @return
     * @throws Exception
     */
    @Override
    public AdminRecord collegeAdminLogin(String account, String password) throws Exception {
        CollegeAdmin collegeAdmin=collegeAdminDao.findCollegeAdmin(account,password);
        //不为空则登陆成功
        if(collegeAdmin!=null){
            AdminRecord adminInfo=new AdminRecord();
            String token= JwtToken.createToken(account,password);
            //创建token,添加私有信息
            adminInfo.setToken(token);
            adminInfo.setCollegeName(collegeDao.findNameByCollegeId(collegeAdmin.getColId()));
            adminInfo.setName(collegeAdmin.getName());
            adminInfo.setAdminId(collegeAdmin.getColId());
            adminInfo.setCollegeId(collegeAdmin.getColId());
            return adminInfo;
        }else{
            return null;
        }
    }

    /**
     * 校管理员登陆
     * @param account 账号
     * @param password 密码
     * @return
     * @throws Exception
     */
    @Override
    public AdminRecord schoolAdminLogin(String account, String password) throws Exception {
        SchoolAdmin schoolAdmin=schoolAdminDao.findSchoolAdmin(account,password);
        //不为空则登陆成功
        if(schoolAdmin!=null){
            //创建token,添加私有信息
            AdminRecord adminRecord=new AdminRecord();
            String token=JwtToken.createToken(account,password);
            adminRecord.setToken(token);
            adminRecord.setAdminId(schoolAdmin.getSchoolAdmin());
            adminRecord.setCollegeId(null);
            return adminRecord;
        }else{
            return null;
        }
    }
}
